package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.StringUtil;
import com.itheima.dao.MenuDao;
import com.itheima.dao.UserDao;
import com.itheima.entity.PageResult;
import com.itheima.pojo.Menu;
import com.itheima.pojo.MenuForMain;
import com.itheima.pojo.User;
import com.itheima.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.JedisPool;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service(interfaceClass = MenuService.class)
@Transactional
public class MenuServiceImpl implements MenuService {
    @Autowired
    private MenuDao menuDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private JedisPool jedisPool;

    @Override
    public List<Menu> findAll() {
        return menuDao.findAll();
    }

    @Override
    public PageResult pageQuery(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage,pageSize);
        Page<Menu> page = menuDao.selectAllParentMenuByCondition(queryString);
        for (Menu menu : page) {
            Integer parentId = menu.getId();
            List<Menu> childrenList=menuDao.selectChildrenListByParentIdId(parentId);
            menu.setChildren(childrenList);
        }
        return new PageResult(page.getTotal(),page.getResult());
    }


    public List<Menu> findAllParentMenu() {
        return menuDao.findAllParentMenu();
    }


    public void add(Menu menu) {
        menuDao.add(menu);
    }

    @Override
    public Menu findById(Integer id) {
        return menuDao.findById(id);
    }

    @Override
    public void edit(Menu menu) {
        menuDao.edit(menu);
    }

    @Override
    public void delete(Integer id) {
        menuDao.delete(id);
    }

    public List<MenuForMain> getAllMenusForCurrentUser(String username) {
        Set<Integer> allIds=menuDao.selectAllMenuForCurrentUser(username);
//        System.out.println(allIds);
        List<MenuForMain> parents=menuDao.getAllParents();
        List<MenuForMain> result=new ArrayList<MenuForMain>();
        for (MenuForMain parent : parents) {
            List<MenuForMain> children=menuDao.getItsChildren(parent.getId());
//            System.out.println(children);
            if(allIds.contains(parent.getId())){
                parent.setChildren(children);
                result.add(parent);
            }else{
                int cnt=0;//计数
                for (MenuForMain child : children) {
                    if (allIds.contains(child.getId())) {
                        cnt++;
                        parent.getChildren().add(child);
                    }
                }
                if (cnt>0) {
                    result.add(parent);
                }
            }
        }
//        System.out.println(result);
        String jsonString = JSON.toJSONString(result);
        jedisPool.getResource().set(username,jsonString);
        return result;
    }

    @Override
    public List<MenuForMain> getAllMenusForCurrentUserFromRedis(String username) {
        String json = jedisPool.getResource().get(username);
        System.out.println(json);
        List<MenuForMain> result=null;
        if(StringUtil.isEmpty(json)){
            System.out.println("来自数据库");
            return this.getAllMenusForCurrentUser(username);
        }else{
            result = (List<MenuForMain>) JSON.parse(json);
            System.out.println(result);
            System.out.println("from Redis");
        }
        return result;
    }


}
